package org.example.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.mapper.ArtifactMapper;
import org.example.mapper.ArtifactsExhibitionTimeMapper;
import org.example.mapper.ExhibitionMapper;
import org.example.ourEnum.ArtifactEnumStatus;
import org.example.ourEnum.ExhibitionStatus;
import org.example.pojo.domain.Artifact;
import org.example.pojo.domain.ArtifactShow;
import org.example.pojo.domain.Exhibition;
import org.example.pojo.dto.ExhibitionDto;
import org.example.pojo.dto.PublishExhibitionDto;
import org.example.pojo.vo.ExhibitionToManyArtifactVo;
import org.example.service.ExhibitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ExhibitionServiceImpl implements ExhibitionService {
    @Autowired
    private ExhibitionMapper exhibitionMapper;

    @Autowired
    private ArtifactMapper artifactMapper;

    @Autowired
    private ArtifactsExhibitionTimeMapper timeMapper;


    @Override
    public int getEventCount() {
        return exhibitionMapper.countEventsToday(new Date());
    }

    @Override
    public Exhibition getExhibitionById(Integer id) {
        return exhibitionMapper.selectById(id);
    }

    @Override
    public List<Exhibition> getAllExhibitions() {
        return exhibitionMapper.selectAll();
    }

    public List<Exhibition> getPublishedExhibitions(){
        return exhibitionMapper.selectPublished();
    }

    @Override
    public Integer createExhibition(Exhibition exhibition) {
        return exhibitionMapper.insert(exhibition);
    }

    @Override
    public Integer updateExhibition(Exhibition exhibition) {
        return exhibitionMapper.update(exhibition);
    }

    @Override
    public Integer deleteExhibition(Integer id) {
        return exhibitionMapper.deleteById(id);
    }

    @Override
    public List<Exhibition> searchExhibitionsByTitle(String title) {
        return exhibitionMapper.searchByName(title);
    }

    public ExhibitionToManyArtifactVo searchDetailById(Integer id){
        Exhibition exhibition = exhibitionMapper.selectById(id);
        List<Artifact> artifacts = artifactMapper.listShowArtifactByExhibitionId(exhibition.getExhibitionId());
        ExhibitionToManyArtifactVo exhibitionToManyArtifactVo = new ExhibitionToManyArtifactVo();
        exhibitionToManyArtifactVo.setExhibition(exhibition);
        exhibitionToManyArtifactVo.setArtifacts(artifacts);
        return exhibitionToManyArtifactVo;
    }

    public void changeStatus(Integer exhibitionId, ExhibitionStatus published){
        // 展览批准
        exhibitionMapper.changeStatus(exhibitionId,published);
        // 文物批准


    }

    public List<Exhibition> getAvailableExhibitions() {
        return exhibitionMapper.findAvailableExhibitions();
    }

    @Override
    @Transactional
    public void addExhibition(ExhibitionDto dto) {
        // 1. 插入 exhibitions 表
        Exhibition ex = new Exhibition();
        ex.setTitle(dto.getTitle());
        ex.setLocation(dto.getLocation());
        ex.setDescription(dto.getDescription());
        ex.setExhibitionType(dto.getExhibitionType());
        ex.setStartTime(dto.getStartTime());
        ex.setEndTime(dto.getEndTime());
        ex.setStatus(ExhibitionStatus.draft); // 默认草稿
        exhibitionMapper.insert(ex);

        Integer exId = ex.getExhibitionId();

        // 2. 对每个 artifactId 插入展览时间记录
        for (Integer artId : dto.getArtifactIds()) {
            ArtifactShow aet = new ArtifactShow();
            aet.setExhibitionId(exId);
            aet.setArtifactId(artId);
            aet.setApprove(false);
            aet.setStartTime(dto.getStartTime());
            aet.setEndTime(dto.getEndTime());
            timeMapper.insertExhibitionTime(aet);
        }
    }

    @Override
    @Transactional
    public void publishExhibition(PublishExhibitionDto dto) {
        // 1. 查询展览以获取时间信息
        Exhibition exh = exhibitionMapper.selectById(dto.getExhibitionId());
        if (exh.getStatus() != ExhibitionStatus.draft) {
            throw new IllegalStateException("只有草稿状态的展览才能发布");
        }

        // 2. 发布展览
        exhibitionMapper.changeStatus(dto.getExhibitionId(), ExhibitionStatus.published);

        // 3. 遍历文物：更新状态 + 关联表
        for (Integer artId : dto.getArtifactIds()) {
            artifactMapper.changeArtifactStatusByArtifactId(artId, ArtifactEnumStatus.loaned);

            // 先尝试 update，若没有记录则 insert
            int updated = timeMapper.updateApproveAndTimes(
                    dto.getExhibitionId(), artId, exh.getStartTime(), exh.getEndTime()
            );
            if (updated == 0) {
                ArtifactShow aet = new ArtifactShow();
                aet.setExhibitionId(dto.getExhibitionId());
                aet.setArtifactId(artId);
                aet.setApprove(true);
                aet.setStartTime(exh.getStartTime());
                aet.setEndTime(exh.getEndTime());
                timeMapper.insertExhibitionTime(aet);
            }
        }
    }

}
